<?php

namespace app\admin\controller\wu;

use app\common\controller\Backend;

use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use think\Db;
use think\Exception;
use think\Session;

/**
 * 環境設備记录管理
 *
 * @icon fa fa-circle-o
 */
class Hjdevicelog extends Backend
{
    
    /**
     * Hjdevicelog模型对象
     * @var \app\admin\model\wu\Hjdevicelog
     */
    protected $model = null;
    const PRODPEC_ID = "__hjdevice_id__";
    protected $noNeedRight = ['quxt'];
    protected $noNeedLogin = ['quxt'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\wu\Hjdevicelog;
        $this->view->assign("commandtypeList", $this->model->getCommandtypeList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        $hjdevice_id = 0;
        if (isset($_SERVER['HTTP_REFERER']) && false !== strpos($_SERVER['HTTP_REFERER'], '/ids/')) {
            $url = $_SERVER['HTTP_REFERER'];
        } else {
            $url = $_SERVER['REQUEST_URI'];
        }

        $hjdevice_Name = "";//装置名称
        $isWsddevice = false;//true-- 溫濕度監控；false---油量溢位與馬達運作監控裝置
        if (false !== strpos($url, '/ids/')) {
            $urls = explode('?', $url);
            $url = $urls[0];
            $urls = explode('/', $url);
            $size = count($urls);
            $hjdevice_id = $urls[$size - 1];
            Session::set(self::PRODPEC_ID, $hjdevice_id);

            $hjdevice = \app\admin\model\wu\Hjdevice::get($hjdevice_id);
            if ($hjdevice) {
                $hjdevice_Name = $hjdevice->name;
                if ($hjdevice_Name == "溫濕度監控") {
                    $isWsddevice = true;
                }
                //Config.isWsddevice
                $this->assignconfig('isWsddevice', $isWsddevice);
            } else {
                Session::set(self::PRODPEC_ID, 0);
            }
        }

        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            if ($hjdevice_id > 0) {
                $total = $this->model
                    ->with(['hjdevice'])
                    ->where($where)
                    ->where('hjdevice_id', $hjdevice_id)
                    ->order($sort, $order)
                    ->count();

                $list = $this->model
                    ->with(['hjdevice'])
                    ->where($where)
                    ->where('hjdevice_id', $hjdevice_id)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            } else {
                $total = $this->model
                    ->with(['hjdevice'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

                $list = $this->model
                    ->with(['hjdevice'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            }

            foreach ($list as $row) {
                
                
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        $this->view->assign("hjdevice_Name", $hjdevice_Name);
        $this->view->assign("hjdevice_id", $hjdevice_id);
        $this->view->assign("isWsddevice", $isWsddevice);

        if ($isWsddevice) {
            $this->view->assign("title", "車間溫濕度紀錄");
        } else {
            $this->view->assign("title", "環境監控紀錄");
        }

        return $this->view->fetch();
    }


    public function quxt($ids)
    {
        $datetimerange = $this->request->request('datetimerange', date('Y-m-d 00:00:00', time()) . " - " . date('Y-m-d H:i:s', time()));

        $datetimes = explode(' - ', $datetimerange);
        $start = "";
        $end = "";
        if (count($datetimes) == 2) {
            $start = $datetimes[0];
            $end = $datetimes[1];
        } else {
            $this->error(__('错误的日期！'));
        }

        $row = \app\admin\model\wu\Hjdevice::get(['id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));

        if ($this->request->isPost()) {
            $this->success("查询成功！");
        }

        $wdlist = $this->model
            ->where("commandtype", "FE00")
            ->where('hjdevice_id', $ids)
            ->where('create_time', '>=', $start)
            ->where('create_time', '<=', $end)
            ->order('id desc')
            ->select();

        $sdlist = $this->model
            ->where("commandtype", "FE01")
            ->where('hjdevice_id', $ids)
            ->where('create_time', '>=', $start)
            ->where('create_time', '<=', $end)
            ->order('id desc')
            ->select();

        $wenlist = $shilist = [];
        $wenXlist = $shiXlist = [];
        foreach ($wdlist as $row1) {
            $wenXlist[] = $row1['create_time'];
            $wenlist[] = $row1['value1'];
        }

        foreach ($sdlist as $row2) {
            $shiXlist[] = $row2['create_time'];
            $shilist[] = $row2['value1'];
        }

        $this->view->assign([
            'wenlist' => array_reverse($wenlist),
            'shilist' => array_reverse($shilist),
            'wenXlist' => array_reverse($wenXlist),
            'shiXlist' => array_reverse($shiXlist)
        ]);


        $this->view->assign("row", $row);
        $this->view->assign("datetimerange", $datetimerange);

        $this->view->assign("title", "車間溫濕度曲線");

        return $this->view->fetch();
    }

    public function zext($ids)
    {
        $datetimerange = $this->request->request('datetimerange', date('Y-m-d 00:00:00', time()) . " - " . date('Y-m-d H:i:s', time()));
        $commandtype = $this->request->request('commandtype', 'A');

        $datetimes = explode(' - ', $datetimerange);
        $start = "";
        $end = "";
        if (count($datetimes) == 2) {
            $start = $datetimes[0];
            $end = $datetimes[1];
        } else {
            $this->error(__('错误的日期！'));
        }

        $row = \app\admin\model\wu\Hjdevice::get(['id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));

        if ($this->request->isPost()) {
            $this->success("查询成功！");
        }

        if ($commandtype === 'A') {
            $mdlist = $this->model
                ->where("commandtype = 'FE04' or commandtype = 'FE05'")
                ->where('hjdevice_id', $ids)
                ->where('create_time', '>=', $start)
                ->where('create_time', '<=', $end)
                ->order('id desc')
                ->select();

        } else {
            $mdlist = $this->model
                ->where("commandtype = 'FE06' or commandtype = 'FE07'")
                ->where('hjdevice_id', $ids)
                ->where('create_time', '>=', $start)
                ->where('create_time', '<=', $end)
                ->order('id desc')
                ->select();
        }


        $mdlist1 = [];
        $mdlist2 = [];
        $mdlist3 = [];
        $mdlist4 = [];
        $mdlist5 = [];
        $mdlist6 = [];
        $mdXlist = [];
        foreach ($mdlist as $row1) {
            $mdXlist[] = $row1['create_time'];
            if ($row1->commandtype == 'FE04' || $row1->commandtype == 'FE06') {
                $mdlist1[] = $row1['value1'];
                $mdlist2[] = $row1['value2'];
                $mdlist3[] = $row1['value3'];
                $mdlist4[] = 0;
                $mdlist5[] = 0;
                $mdlist6[] = 0;
            } else {
                $mdlist1[] = 0;
                $mdlist2[] = 0;
                $mdlist3[] = 0;
                $mdlist4[] = $row1['value1'];
                $mdlist5[] = $row1['value2'];
                $mdlist6[] = $row1['value3'];
            }
        }

        $this->view->assign([
            'mdlist1' => array_reverse($mdlist1),
            'mdlist2' => array_reverse($mdlist2),
            'mdlist3' => array_reverse($mdlist3),
            'mdlist4' => array_reverse($mdlist4),
            'mdlist5' => array_reverse($mdlist5),
            'mdlist6' => array_reverse($mdlist6),
            'mdXlist' => array_reverse($mdXlist),
        ]);

        $this->view->assign("row", $row);
        $this->view->assign("datetimerange", $datetimerange);
        $this->view->assign("commandtype", $commandtype);
        $this->view->assign("title", "抽油馬達運轉曲線");

        return $this->view->fetch();
    }

}
